package com.zxh.zhenxihuan.service.impl;

import com.zxh.zhenxihuan.entity.z.OrderForm;
import com.zxh.zhenxihuan.mapper.OrderFormMapper;
import com.zxh.zhenxihuan.service.OrderFormService;
import com.zxh.zhenxihuan.util.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("orderFormService")
public class OrderFormServiceImpl implements OrderFormService {
    @Resource
    private OrderFormMapper orderFormMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout = -1, rollbackFor = Exception.class)
    public Page<OrderForm> page(Integer num, Integer count) {
        Integer pageNum = 1;
        try {
            pageNum = Integer.valueOf(num);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Integer pageCount = 5;
        try {
            pageCount = Integer.valueOf(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Page<OrderForm> page = new Page<>();
        page.init(pageNum, pageCount, getSum());
        System.out.println(getSum());
        page.setList(getOrderForm(page.getRowNum(), pageCount));
        return page;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout = -1, rollbackFor = Exception.class)
    public Integer getSum() {
        Integer num = orderFormMapper.getSum();
        return num;
    }

    @Override
    public List<OrderForm> getOrderForm(Integer start, Integer end) {
        List<OrderForm> list = orderFormMapper.getOrderForm(start, end);
        for (OrderForm orderForm : list) {
            System.out.println(orderForm);
        }
        return list;
    }

    @Override
    public Integer del(Integer id) {
        Integer num = orderFormMapper.del(id);
        return num;
    }

    @Override
    public OrderForm addOrderForm(OrderForm orderForm) {
        try {
            orderForm.setClear_time(new Date());
            orderForm.setStatus(1L);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Long id = orderFormMapper.add(orderForm);
        if(id == 0) {
            return null;
        }
        return orderForm;
    }

    @Override
    public OrderForm update(OrderForm orderForm) {
        long id = orderFormMapper.update(orderForm);
        if(id>0){
            return orderForm;
        }
        return null;
    }

//    @Override
//    public Map<String, Object> listOrder(Integer page, Integer count) {
//        List<OrderForm>list=orderFormMapper.getOrderForm(page,count);
//        Map<String, Object> map=new HashMap<>();
//        map.put("order",list);
//        return map;
//    }
}
